Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make reinitialize_command's return type Generic when "command" argument is a Command #308

Merged
merged 1 commit into from
Dec 27, 2024

Conversation

Avasam
Copy link
Contributor

@Avasam Avasam commented Oct 28, 2024

This is a part of adding typeshed's typevars to setuptools, but this change is better handled on distutils' side rather than adding typing-only overloaded overrides in setuptools.

This won't have any effect outside this repo and pypa/setuptools#4704 until pypa/setuptools#4689 , but it also shouldn't have any negative effect by itself.

@Avasam Avasam force-pushed the Generic-reinitialize_command branch from b48308e to 4a1612b Compare October 28, 2024 18:38
@Avasam Avasam force-pushed the Generic-reinitialize_command branch from 4a1612b to 5d2c034 Compare November 24, 2024 20:19
@jaraco
Copy link
Member

jaraco commented Dec 26, 2024

Can you explain why these overloads are necessary? In particular, why is the Generic necessary? What is the difference between accepting a Command and a CommandT (bound to Command)?

@Avasam
Copy link
Contributor Author

Avasam commented Dec 26, 2024

Can you explain why these overloads are necessary? In particular, why is the Generic necessary? What is the difference between accepting a Command and a CommandT (bound to Command)?

With generic:

command: Command = ...
install_lib_command: install_lib = ...
reinitialized_command = command.reinitialize_command(install_lib_command)
# Mypy: Revealed type is "distutils.command.install_lib.install_lib"
# Pyright: Type of "reinitialized_command" is "install_lib"
reveal_type(reinitialized_command)

Without generic:

command: Command = ...
install_lib_command: install_lib = ...
reinitialized_command = command.reinitialize_command(install_lib_command)
# Mypy: Revealed type is "distutils.cmd.Command"
# Pyright: Type of "reinitialized_command" is "Command"
reveal_type(reinitialized_command)

The generic allows consumers of this method to not loose on type specificity. Otherwise you'd have to typing.cast back to the original type to use its subclass-specific features. (as can be seen in setuptools)

@Avasam Avasam force-pushed the Generic-reinitialize_command branch from 5d2c034 to 16dbe11 Compare December 26, 2024 17:51
@Avasam Avasam force-pushed the Generic-reinitialize_command branch from 16dbe11 to 2017969 Compare December 26, 2024 23:51
@jaraco jaraco merged commit e6d3191 into pypa:main Dec 27, 2024
22 checks passed
@Avasam Avasam deleted the Generic-reinitialize_command branch December 27, 2024 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants